package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.CourseCategory;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.CourseCategoryMapper;
import com.jyy.prefabricated.utils.TreeBuilder;
import com.jyy.prefabricated.vo.CourseCategoryData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class CourseCategoryService {

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    /**
     * 新增
     *
     * @param courseCategory
     * @return
     */
    public int insert(CourseCategory courseCategory) {
        if (courseCategory.getPid() == null) {
            courseCategory.setPid(0);
        }
        courseCategoryMapper.insert(courseCategory);
        return courseCategory.getId();
    }

    /**
     * 更新
     *
     * @param courseCategory
     * @return
     */
    public int update(CourseCategory courseCategory) {
        if (courseCategory.getId() == null) {
            throw new CustomException("参数id不能为空");
        }
        checkCategoryLevel(courseCategory);
        return courseCategoryMapper.updateByPrimaryKeySelective(courseCategory);
    }

    /**
     * 校验层级
     *
     * @param category
     */
    public void checkCategoryLevel(CourseCategory category) {
        List<CourseCategory> categories = selectAll();
        List<CourseCategory> son = findCategoryIdByPid(category.getId(), categories);
        List<Integer> ids = son.stream().map(e -> e.getId()).collect(Collectors.toList());
        if (ids.contains(category.getPid()) || category.getId().equals(category.getPid())) {
            throw new CustomException("当前分类的父类不能为它的子分类和它本身");
        }
    }

    /**
     * 根据父级id所有子级分类id
     *
     * @param pid
     * @param categories
     * @return
     */
    public List<CourseCategory> findCategoryIdByPid(Integer pid, List<CourseCategory> categories) {
        List<CourseCategory> list = new ArrayList<>();
        categories.stream().filter(e -> pid.equals(e.getPid())).forEach(item -> {
            list.add(item);
            List<CourseCategory> son = findCategoryIdByPid(item.getId(), categories);
            list.addAll(son);
        });
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        return courseCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public CourseCategory selectById(Integer id) {
        return courseCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有列表
     *
     * @return
     */
    public List<CourseCategory> selectAll() {
        return courseCategoryMapper.selectAll();
    }

    /**
     * 根据参数查询列表
     *
     * @param category 分类名称
     * @param pid      父级分类id
     * @return
     */
    public List<CourseCategory> selectByParams(String category, Integer pid , Integer type) {
        return courseCategoryMapper.selectByParams(category, pid ,type);
    }

    public List<CourseCategoryData> selectTree(Integer pid) {
        List<CourseCategoryData> list = courseCategoryMapper.queryTreeNode();
        return TreeBuilder.bulid(list, pid);
    }
}
